﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.Context;
using OFB.BLL.ViewModel;
using System.Data;
using OFB.Domain;

namespace OFB.BLL.Repository
{
    public class HistoryFeedbackBLL : IHistoryFeedbackBLL
    {

        private HistoryFeedbackContext context;
        public HistoryFeedbackBLL()
        {
            context = new HistoryFeedbackContext();
        }

        #region IHistoryFeedbackBLL Members

        public ICollection<TraineeHistoryFeedbackViewModel> FindAllHistoryFeedback(string traineeId)
        {

            return context.HistoryFeedback.Init("HistoryFeedback_Get", context.HistoryFeedback.BuildParameter("TraineeId", DbType.String, traineeId))
                                            .MapCollection<TraineeHistoryFeedbackViewModel>()
                                            .GetCollection();
        }

        #endregion

        #region IHistoryFeedbackBLL Members


        public TraineeHistoryFeedbackViewModel SearchByFbDetailId(int fbDetailId)
        {
            return context.HistoryFeedback.Init("HistoryFeedback_GetInformation", context.HistoryFeedback.BuildParameter("FbDetailId", DbType.UInt32, fbDetailId))
                                                .Map()
                                                .Get();
        }

        #endregion

        #region IHistoryFeedbackBLL Members


        public ICollection<QuestionAnswerViewModel> ViewFeedbackAnswerDetail(string traineeId, int courseId, int fbDetailId)
        {
            return context.QuestionAnswer.Init("HistoryFeedback_GetAnswerUpdateDetail", context.QuestionAnswer.BuildParameter("TraineeId", DbType.String, traineeId),
                                                                            context.QuestionAnswer.BuildParameter("CourseId", DbType.UInt32, courseId),
                                                                            context.QuestionAnswer.BuildParameter("FbDetailId", DbType.UInt32, fbDetailId))
                                                                          .MapCollection<QuestionAnswerViewModel>()
                                                                          .GetCollection();

        }

        #endregion

        #region IHistoryFeedbackBLL Members


        public FeedbackAnswer GetAdditionalComment(int fbDetailId)
        {
            return context.FeedbackAnswer.Init("HistoryFeedback_GetComment", context.FeedbackAnswer.BuildParameter("FbDetailId", DbType.Int32, fbDetailId))
                                    .Map()
                                    .Get();
        }

        #endregion

        #region IHistoryFeedbackBLL Members


        public bool SaveFeedbackDetail(int detailId, int questionId, double mark, string comment)
        {
            IDataParameter rowEffectedParam = context.QuestionAnswer.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.QuestionAnswer.Execute("HistoryFeedback_SaveWithoutSubmit", context.QuestionAnswer.BuildParameter("FbDetailId", DbType.Int32, detailId),
                                               context.QuestionAnswer.BuildParameter("QuestionId", DbType.Int32, questionId),
                                               context.QuestionAnswer.BuildParameter("Mark", DbType.Double, mark),
                                               context.QuestionAnswer.BuildParameter("Comment", DbType.String, comment),
                                               rowEffectedParam);

            return (int) rowEffectedParam.Value == 0 ? false : true;
        }

        #endregion

        #region IHistoryFeedbackBLL Members


        public bool SaveFeedbackComment(int detailId, string comment)
        {
            IDataParameter rowEffectedParam = context.HistoryFeedback.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.HistoryFeedback.Execute("HistoryFeedback_SaveComment", context.HistoryFeedback.BuildParameter("FbDetailId", DbType.Int32, detailId),
                                               context.HistoryFeedback.BuildParameter("Comment", DbType.String, comment),
                                               rowEffectedParam);

            return (int)rowEffectedParam.Value == 0 ? false : true;
        }

        #endregion

        #region IHistoryFeedbackBLL Members


        public bool Submit(int detailId, int questionId, double mark, string comment)
        {
            IDataParameter rowEffectedParam = context.QuestionAnswer.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.QuestionAnswer.Execute("HistoryFeedback_Submit", context.QuestionAnswer.BuildParameter("FbDetailId", DbType.Int32, detailId),
                                               context.QuestionAnswer.BuildParameter("QuestionId", DbType.Int32, questionId),
                                               context.QuestionAnswer.BuildParameter("Mark", DbType.Double, mark),
                                               context.QuestionAnswer.BuildParameter("Comment", DbType.String, comment),
                                               rowEffectedParam);

            return (int)rowEffectedParam.Value == 0 ? false : true;
        }

        #endregion

        #region IHistoryFeedbackBLL Members


        public ICollection<TraineeHistoryFeedbackViewModel> FindAllHistoryFeedbackPage(string traineeId, int startRow, int maxRow, out int totalRow)
        {
            IDataParameter para = context.HistoryFeedback.BuildParameter("totalRows", DbType.Int32, ParameterDirection.Output);
            totalRow = 0;
            ICollection<TraineeHistoryFeedbackViewModel> lst = context.HistoryFeedback.Init("HistoryFeedback_GetPage", context.HistoryFeedback.BuildParameter("TraineeId", DbType.Int32, traineeId),
                                                                                                                    context.HistoryFeedback.BuildParameter("startRowIndex", DbType.Int32, startRow),
                                                                                                                    context.HistoryFeedback.BuildParameter("maximumRows", DbType.Int32, maxRow),
                                                                                                                    para)
                                                                                                                    .MapCollection<TraineeHistoryFeedbackViewModel>()
                                                                                                                    .GetCollection();
            totalRow = Convert.ToInt32(para.Value);
            return lst;
        }

        #endregion


        #region IHistoryFeedbackBLL Members


        public HistoryFeedbackTraineeCourseViewModel GetTraineeCourse(string username)
        {
            return context.TraineeCourse.Init("HistoryFeedback_GetTraineeCourse", context.TraineeCourse.BuildParameter("Username", DbType.String, username))
                                            .Map()
                                            .Get();
        }

        #endregion
    }
}
